class Solution
{
public:
    bool validateBinaryTreeNodes(int n, vector<int> &leftChild, vector<int> &rightChild)
    {
        vector<int> inDegree(n, 0);
        for (int child : leftChild)
        {
            if (child != -1)
            {
                inDegree[child] += 1;
            }
        }
        for (int child : rightChild)
        {
            if (child != -1)
            {
                inDegree[child] += 1;
            }
        }

        int root = -1;
        for (int i = 0; i < n; ++i)
        {
            if (inDegree[i] == 0)
            {
                root = i;
            }
        }
        if (root == -1)
        {
            return false;
        }

        unordered_set<int> used;
        queue<int> q;
        q.push(root);
        while (!q.empty())
        {
            int node = q.front();
            q.pop();
            if (used.count(node))
            {
                return false;
            }
            else
            {
                used.insert(node);
            }
            if (leftChild[node] != -1)
            {
                q.push(leftChild[node]);
            }
            if (rightChild[node] != -1)
            {
                q.push(rightChild[node]);
            }
        }
        return used.size() == n;
    }
};